/*
 *  This file is part of Social Media Monitoring Toolbox.
 *
 *  Social Media Monitoring Toolbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Social Media Monitoring Toolbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Social Media Monitoring Toolbox. If not, see <http://www.gnu.org/licenses/lgpl.html>.
 *
 * � Copyright 2013
 * Jana Asmussen, Julian Bau�, Daniela Dalock, Christian Gutjahr,
 * Fabian Heidorn, Alexander Kaack, Vitali Kagadij 
 * 
 */

package gui;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.faces.bean.ManagedBean;
import javax.faces.event.ActionEvent;

import org.primefaces.model.chart.CartesianChartModel;
import org.primefaces.model.chart.ChartSeries;
import org.primefaces.model.chart.LineChartSeries;

import types.FilterTyp;
import types.StreamDescriptor;
import types.TweetPost;
import types.YoutubeVideoType;

@ManagedBean(name = "chartLineBean")
public class ChartLineBean implements Serializable {

	private static final long serialVersionUID = -340465857009772274L;
	private GetData data = new GetData();
	private String period = "1";
	private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
	private Date from;
	private Date to;
	private CartesianChartModel linearModel;

	public ChartLineBean() {
		createLinearModel();
	}

	/**
	 * Creates a new linear model for the chart in the application. Sets the
	 * values to a default value
	 * 
	 */
	private void createLinearModel() {
		linearModel = new CartesianChartModel();

		LineChartSeries series1 = new LineChartSeries();
		series1.setLabel("Average");
		
		Date date = new Date();
		series1.set(dateFormat.format(date), 0);

		linearModel.addSeries(series1);
	}
	
	/**
	 * Update the line chart
	 * 
	 * @param ae
	 *            ActionEvent, that triggers this method
	 * @param id
	 *  		  id of the stream
	 * @param interval
	 * 			  the interval between the date-points
	 * @param from
	 * 			  the start date
	 * @param to
	 * 			  the end date
	 */
	public void updateLineChart(ActionEvent ae, String id, String interval, Date from, Date to) {
		
		
		List<StreamDescriptor> streams = data.getStreams();
		StreamDescriptor stream = data.getStreamDescriptor(id, streams);
		if(id!="" && interval!="" && from!=null && to!=null){
			TreeMap<Date, Double> values = data.getAverageSentimentForPeriod(stream, from, to, Integer.parseInt(interval));
			if(!values.isEmpty()){
				linearModel = null;
				linearModel = new CartesianChartModel();				
				LineChartSeries series1 = new LineChartSeries();
				series1.setLabel("Average");
				
				if(Integer.parseInt(interval)==30){
					dateFormat = new SimpleDateFormat("yyyy MMMM", Locale.ENGLISH);
				}else if(Integer.parseInt(interval)==360){
					dateFormat = new SimpleDateFormat("yyyy");
				}
			int checkvalue = 0;
			for (Entry<Date, Double> sentimentAverageValue : values.entrySet()) {
				if(sentimentAverageValue.getValue() != -11){
					series1.set(dateFormat.format(sentimentAverageValue.getKey()), sentimentAverageValue.getValue());
				}else{
					checkvalue++;
				}				
			}
			if(checkvalue==values.size()){
				Date date = new Date();
				series1.set(dateFormat.format(date), 0);
			}
				
			linearModel.addSeries(series1);
			}
		}else{
			System.out.println("Someting is missing!");
		}
	}
	
	/**
	 * Setting the period - 
	 * 
	 * @param period
	 *            selected interval 30 or 60 or 360
	 */
	public void setPeriod(String period) {
		this.period = period;
	}
	
	/**
	 * Setting the start date - 
	 * 
	 * @param from
	 *            start date
	 */
	public void setfrom(Date from) {
		this.from = from;
	}

	/**
	 * Setting the end date - 
	 * 
	 * @param to
	 *            end date
	 */
	public void setto(Date to) {
		this.to = to;
	}

	/**
	 * Returns the period / selected interval
	 * 
	 * @return period / 30 or 60 or 360
	 */
	public String getPeriod() {
		return period;
	}	

	/**
	 * Returns the start date
	 * 
	 * @return start date 
	 */
	public Date getfrom() {
		return from;
	}
	
	/**
	 * Returns the end date
	 * 
	 * @return end date
	 */
	public Date getto() {
		return to;
	}	
	
	/**
	 * Getter for the LineModel of the
	 * 
	 * @return CartesianChartModel
	 */
	public CartesianChartModel getLinearModel() {
		return linearModel;
	}
}